﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using NLog.Web;

namespace Powerpi
{
    public class Program
    {

        public static void Main(string[] args)
        {
           
            IHost webHost = CreateHostBuilder(args).Build();

            webHost.Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args)
        {
            var config = GetHostConfig();
            var builder = Host.CreateDefaultBuilder(args)
                  .ConfigureWebHostDefaults(webBuilder =>
                  {
                      webBuilder.UseStartup<Startup>()
                                .UseUrls(config["Server:Urls"])
                                .UseEnvironment(config["Server:EnvironmentName"])
                                .ConfigureLogging(logging =>
                                {
                                    //logging.ClearProviders();
                                    //logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Information);
                                    //logging.AddConsole();
                                    logging.AddNLog($"Configs/nlog.{config["Server:EnvironmentName"]}.config");
                                }).UseNLog()
                                ;
                  });
            return builder;
        }
        /// <summary>
        /// 主要环境信息
        /// </summary>
        /// <returns></returns>
        public static IConfigurationRoot GetHostConfig()
        {
            return new ConfigurationBuilder()
               .SetBasePath(Environment.CurrentDirectory)
               .AddJsonFile("hosting.json", optional: true)
               .Build();
        }
    }
}
